<script setup lang="ts">
import {
  LexicalComposer,
  LexicalPlainTextPlugin,
  LexicalContentEditable,
  LexicalClearEditorPlugin,
  LexicalOnChangePlugin
} from 'lexical-vue'
import { $getRoot, $createParagraphNode, $createTextNode, EditorState } from 'lexical'
import ShortcutMenuPlugin from '@/views/LLMStudio2/components/InputEditor/plugins/ShortcutMenuPlugin/index.vue'
import SendPlugin from '@/views/LLMStudio2/components/InputEditor/plugins/SendPlugin.vue'
import ClearPlugin from '@/views/LLMStudio2/components/InputEditor/plugins/ClearPlugin.vue'
import CustomErrorBoundaryPlugin from '@/views/LLMStudio2/components/InputEditor/plugins/CustomErrorBoundaryPlugin.vue'
import { ref } from 'vue'

interface Props {
  height: number
  editorValue: string
}

const sendBtnRef = ref()
const clearEditorRef = ref()
const props = withDefaults(defineProps<Props>(), {
  height: 250,
  editorValue: ''
})

const emit = defineEmits<{
  (e: 'update:editorValue', value: string): void
  (e: 'send', value: string): void
  (e: 'newChat'): void
}>()

const exampleTheme = {
  ltr: 'ltr',
  rtl: 'rtl',
  placeholder: 'editor-placeholder',
  paragraph: 'editor-paragraph'
}

const editorConfig = {
  theme: exampleTheme,
  editable: true,
  onError(error: Error) {
    console.error(error)
  },
  nodes: [],
  editorState: () => {
    const root = $getRoot()
    if (root.getFirstChild() === null) {
      const paragraph = $createParagraphNode()
      paragraph.append($createTextNode(''))
      root.append(paragraph)
    }
  }
}
const handleSend = (content: string) => {
  emit('send', content)
  setTimeout(() => {
    clearEditorRef.value.clearEditor()
  }, 10)
}

const onChange = (editorState1: EditorState) => {
  editorState1.read(() => {
    const root = $getRoot()
    const text = root.getTextContent()
    emit('update:editorValue', text)
  })
}

const handleKeyDown = (event: KeyboardEvent) => {
  if (event.key === 'Enter' && !event.ctrlKey && !event.shiftKey) {
    sendBtnRef.value.handleSend()
  }
}
const handleNewChat = () => {
  emit('newChat')
}
</script>

<template>
  <LexicalComposer :initial-config="editorConfig">
    <CustomErrorBoundaryPlugin>
      <div class="editor-container">
        <LexicalPlainTextPlugin>
          <template #contentEditable>
            <LexicalContentEditable
              class="editor-input"
              :style="{ 'min-height': `${props.height}px` }"
              @keydown="handleKeyDown"
            />
          </template>
          <template #placeholder>
            <div class="editor-placeholder">
              高质量的提问更具价值，您可以输入'/' 调用常用高质量命令
            </div>
          </template>
        </LexicalPlainTextPlugin>
      </div>
      <LexicalOnChangePlugin @change="onChange" />
      <ShortcutMenuPlugin />
      <LexicalClearEditorPlugin />
      <ClearPlugin ref="clearEditorRef" />
      <SendPlugin ref="sendBtnRef" @send="handleSend" @newChat="handleNewChat" />
    </CustomErrorBoundaryPlugin>
  </LexicalComposer>
</template>

<style lang="scss" scoped>
.editor-container {
  background: #fff;
  border-radius: 2px;
  color: #000;
  position: relative;
  line-height: 20px;
  font-weight: 400;
  text-align: left;
  border-top-left-radius: 10px;
  border-top-right-radius: 10px;
}

.editor-input {
  padding: 10px;
  resize: none;
  font-size: 15px;
  caret-color: #444;
  position: relative;
  tab-size: 1;
  outline: 0;
}

.editor-placeholder {
  color: #999;
  overflow: hidden;
  position: absolute;
  text-overflow: ellipsis;
  top: 10px;
  left: 10px;
  font-size: 13px;
  user-select: none;
  display: inline-block;
  pointer-events: none;
}

.editor-paragraph {
  margin: 0 0 15px 0;
  position: relative;
}
</style>
